#include"sys.h"

/**
 * @brief print_head
 * 打印命令行头
 * @author Micros0ft
 * @return int 
 */

void print_head(){
// whoami@umane -n : pwd $
    FILE *shell;
    char loginname[20];
    char hostname[20];
    char pathname[100];
    shell = popen("echo -n `whoami`","r");
    fgets(loginname,sizeof(loginname),shell);
    shell = popen("echo -n `uname -n`","r");
    fgets(hostname,sizeof(hostname),shell);
    shell = popen("echo -n `pwd`","r");
    fgets(pathname,sizeof(pathname),shell);

    printf("\033[32m\033[01m%s@%s:%s$ \033[0m",loginname,hostname,pathname);
    pclose(shell);
}

/**
 * @brief 打印mysh信息
 * @author Micros0ft
 */
void print_sh_info(){
    printf("Welcome to mush!\n");
    printf("Version:1.0\n");
    printf("Author:Micros0ft\n");
    printf("github:\n");
}


/**
 * @brief get_shell
 * 获取shell内容,并将获取的内容赋给传入的指针buffer
 * @autuor: Micros0ft
 * @return int
 */
void get_shell(char* buffer){
    print_head();
    gets(buffer);
    // fgets(buffer,100,stdin);//获取命令 切记 字符串之间有空格用gets puts(buffer);
}

/**
 * @brief command
 * 向终端发送命令，并接收终端返回的内容
 * command函数使用的是popen管道
 * @author:Micros0ft
 */
void command(){
    pid_t pid;
    int cnt = 0;
    pid = vfork();
    if(pid<0)
        printf("error in fork!\n");
    else if(pid == 0)
    {
        // printf("I am the child process,ID is %d\n",getpid());`   
        char comm[100] = {0};
        get_shell(comm);
        printf("%s\n", comm);
        char sendline[] = "echo `";
        strcat(sendline, comm);
        strcat(sendline, "`");
        printf("sendline =  %s\n", sendline);
        FILE* fp = popen(sendline, "r");
        char buffer[100] = {0};  
        fgets(buffer, sizeof(buffer), fp);
        char* token;
        token = strtok(buffer, " ");
        while (token != NULL){
            printf("%s\n",token);
		    token=strtok(NULL, " ");
        }
        
    }
    exit(0);
}

/**
 * @brief 调用execve和ececvp实现外部命令和内部命令的调用
 * 
 * @return int 
 */
int my_execv(){
    char comm[100] = {0};
    get_shell(comm);
    char* arg[6] = {0};

    int ret = 0;

    char* token;
    token = strtok(comm, " ");
    arg[0] = token;//命令和第0个参数

    if(!strcmp(arg[0], "exit")){
        printf("程序即将退出！");
        return -1;
    }
    int i = 0;
    while (token != NULL){
        arg[i] = token;
        token=strtok(NULL, " ");
        i++;
    }
    // for(int i = 0; i < 3; i++){
    //     printf("%d:%s\n", i, arg[i]);
    // }
    if(execvp(arg[0], arg) < 0){
        if(errno == 2){
            // printf("不是内部命令，即将加载.myshrc");//加载.myshrc
            // FILE* myshrc = fopen("./.myshrc", "r");
            // char myshrc_line = fgets(myshrc_line, 100, myshrc);
            // char* command_line;
            // strcat(command_line, myshrc);
            // strcat(command_line, arg[0]);
            // printf("commandline is %s", command_line);
            // if(execve(command_line, NULL, NULL) < 0){
            //     printf("%s:Command not found\n", arg[0]);
            // }
            printf("%s:Command not found\n", arg[0]);
        }else{
            printf("execvp error %d %s\n",errno,strerror(errno));
            exit(errno);
        }
    }
    return ret;
}

int run(){
    int ret = 0;
    pid_t pid;
    pid = vfork();
    if(pid < 0){
        printf("fork error!");
    }else if(pid == 0){
        ret = my_execv();
        exit(0);
    }
    else{
        waitpid(pid, NULL, 0);
    }
    //printf("run return = %d", ret);
    return ret;
}